package com.gxa.controller;

import com.gxa.common.base.Result;
import com.gxa.domain.entity.StockHistory;
import com.gxa.domain.vo.StockHistoryVO;
import com.gxa.service.StockHistoryService;
import com.gxa.common.util.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stock/history")
@Tag(name = "上货历史记录", description = "上货历史记录管理相关接口")
public class StockHistoryController {

    @Autowired
    private StockHistoryService stockHistoryService;

    @PostMapping("/insert")
    @Operation(summary = "插入上货历史记录")
    public Result<Integer> insert(@RequestBody StockHistory stockHistory) {
        try {
            int result = stockHistoryService.insert(stockHistory);
            return Result.buildSuccess(result);
        } catch (Exception e) {
            return Result.buildFail(500, "插入上货历史记录失败: " + e.getMessage());
        }
    }

    @PostMapping("/batchInsert")
    @Operation(summary = "批量插入上货历史记录")
    public Result<Integer> batchInsert(@RequestBody List<StockHistory> stockHistoryList) {
        try {
            System.out.println("StockHistoryController.batchInsert - 接收到的数据: " + stockHistoryList);

            if (stockHistoryList == null || stockHistoryList.isEmpty()) {
                return Result.buildFail(400, "上货历史记录列表不能为空");
            }

            // 验证数据完整性
            for (StockHistory history : stockHistoryList) {
                if (history.getBarcodeNo() == null || history.getBarcodeNo().trim().isEmpty()) {
                    return Result.buildFail(400, "商品条码不能为空");
                }
                if (history.getQuantity() == null || history.getQuantity() <= 0) {
                    return Result.buildFail(400, "上货数量必须大于0");
                }
                if (history.getOperatorId() == null || history.getOperatorId() <= 0) {
                    return Result.buildFail(400, "操作员ID不能为空");
                }
            }

            int result = stockHistoryService.batchInsert(stockHistoryList);
            System.out.println("StockHistoryController.batchInsert - 插入结果: " + result);
            return Result.buildSuccess(result);
        } catch (Exception e) {
            System.err.println("StockHistoryController.batchInsert - 异常: " + e.getMessage());
            e.printStackTrace();
            return Result.buildFail(500, "批量插入上货历史记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询上货历史记录")
    public Result<Page> selectPage(@RequestParam Map<String, Object> params) {
        try {
            System.out.println("StockHistoryController.selectPage - 接收到的参数: " + params);
            Page page = stockHistoryService.selectPage(params);
            System.out.println("StockHistoryController.selectPage - 查询结果: list.size=" + (page.getList() != null ? page.getList().size() : 0) + ", totalCount=" + page.getTotalCount());
            return Result.buildSuccess(page.getList(), page.getTotalCount());
        } catch (Exception e) {
            System.err.println("StockHistoryController.selectPage - 异常: " + e.getMessage());
            e.printStackTrace();
            return Result.buildFail(500, "查询上货历史记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/barcode/{barcodeNo}")
    @Operation(summary = "根据商品条码查询上货历史记录")
    public Result<List<StockHistoryVO>> selectByBarcodeNo(@PathVariable String barcodeNo) {
        try {
            List<StockHistoryVO> list = stockHistoryService.selectByBarcodeNo(barcodeNo);
            return Result.buildSuccess(list);
        } catch (Exception e) {
            return Result.buildFail(500, "查询上货历史记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/operator/{operatorId}")
    @Operation(summary = "根据操作员ID查询上货历史记录")
    public Result<List<StockHistoryVO>> selectByOperatorId(@PathVariable Integer operatorId) {
        try {
            List<StockHistoryVO> list = stockHistoryService.selectByOperatorId(operatorId);
            return Result.buildSuccess(list);
        } catch (Exception e) {
            return Result.buildFail(500, "查询上货历史记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/timerange")
    @Operation(summary = "根据时间范围查询上货历史记录")
    public Result<List<StockHistoryVO>> selectByTimeRange(
            @RequestParam String startTime,
            @RequestParam String endTime) {
        try {
            List<StockHistoryVO> list = stockHistoryService.selectByTimeRange(startTime, endTime);
            return Result.buildSuccess(list);
        } catch (Exception e) {
            return Result.buildFail(500, "查询上货历史记录失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除单条上货历史记录")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        try {
            int result = stockHistoryService.deleteById(id);
            return Result.buildSuccess(result);
        } catch (Exception e) {
            return Result.buildFail(500, "删除上货历史记录失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/batchDelete")
    @Operation(summary = "批量删除上货历史记录")
    public Result<Integer> batchDelete(@RequestBody Map<String, List<Integer>> idsMap) {
        try {
            List<Integer> ids = idsMap.get("ids");
            int result = stockHistoryService.batchDelete(ids);
            return Result.buildSuccess(result);
        } catch (Exception e) {
            return Result.buildFail(500, "批量删除上货历史记录失败: " + e.getMessage());
        }
    }

}